/**
 * @(#) InrecMediator.as
 *
 * @date		2013. 05. 30.
 * @version		1.0
 * @author		Park, Jutae
 * @description	콘도추천 신청 - Mediator
 * 				Inrec를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.condoreq.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.condoreq.CondoFacade;
	import com.skt.common_hr.condoreq.events.ComboBoxAreaEvent;
	import com.skt.common_hr.condoreq.events.ComboBoxRoomsEvent;
	import com.skt.common_hr.condoreq.model.CondoProxy;
	import com.skt.common_hr.condoreq.model.vo.CondoVO;
	import com.skt.common_hr.condoreq.view.components.CondoreqView;
	import com.skt.common_hr.condoreq.view.components.ReqCondo;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class CondoMediator extends OverViewMediator
	{
		public static const NAME:String = "InrecMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function CondoMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
			super.initializeView();
//			loadYear();
 		}
		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			view.setDateRange();
 			loadList();
 		} 
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(ReqCondo.Insert, onSinChung);
 			view.addEventListener(CondoreqView.Delete, onDelete);
 			view.addEventListener(CondoreqView.Serach, loadList);
 			view.addEventListener(ReqCondo.CONDO_PROFILE, onReadCheck);
 			view.addEventListener(ReqCondo.CONDO_COMBO, onCondoComboSearch);
 			view.addEventListener(CondoreqView.dgClick, onRead);
 			view.reqView.addEventListener(ComboBoxAreaEvent.SELECT_AREA_DATA, onAreaCombo);
 			view.reqView.addEventListener(ComboBoxRoomsEvent.SELECT_ROOMS_DATA, onRoomsCombo);
 			
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(ReqCondo.Insert, onSinChung);
 			view.removeEventListener(CondoreqView.Delete, onDelete);
 			view.removeEventListener(CondoreqView.Serach, loadList);
 			view.removeEventListener(ReqCondo.CONDO_PROFILE, onReadCheck);
 			view.removeEventListener(ReqCondo.CONDO_COMBO, onCondoComboSearch);
 			view.removeEventListener(CondoreqView.dgClick, onRead);
 			view.reqView.removeEventListener(ComboBoxRoomsEvent.SELECT_ROOMS_DATA, onRoomsCombo);
 			view.reqView.removeEventListener(ComboBoxAreaEvent.SELECT_AREA_DATA, onAreaCombo);
 		}
 		
 		/* 콘도리스트 클릭시 List 조회 */
 		private function loadList(evt:Event = null):void 
 		{
 			view.vs.selectedIndex = 0;
 			var proxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
 			proxy.load(Variables.myPernr, view.sDay, view.eDay);
 		}
 		
 		/* 콘도 신청 저장 클릭시 */
 		private function onSinChung(e:Event):void
 		{
 			var obj:CondoVO = view.reqView.dataVoList;
			
 			var proxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
 			proxy.insert(obj,view.reqView.fileListArr);
 		}
 		
 		/* 콘도 신청 삭제 클릭시 */
		private function onDelete(event:Event):void
		{
			var obj:CondoVO = view.dg.selectedItem as CondoVO;
			
 			var proxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
 			proxy.del(Variables.myPernr, obj); 
			
		}
		
		private function onRead(event:Event):void
		{
			var _vo:CondoVO = new CondoVO();
 				_vo =  view.dg.selectedItem as CondoVO;
			
			var proxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
 			proxy.readLoad(_vo);
		}
 		
		/* 콘도신청 조회 신청여부 체크 클릭시 */
		private function onReadCheck(event:Event):void
		{
			var proxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
			
 			proxy.loadProfile(Variables.myPernr);
		}
		
		/* 콘도 콤보박스 조회 */
		private function onCondoComboSearch(event:Event):void
 		{
 			var objDate:Date = view.reqView.sDay;
 			
 			var statementProxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
			statementProxy.loadCondoCombo(objDate);
 		}
 		
 		/* 지역 콤보박스 변경시 */
 		private function onAreaCombo(event:ComboBoxAreaEvent):void
 		{
 			var objDate:Date = view.reqView.sDay;
 				
 			var cbProxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
			cbProxy.loadAreaCombo(objDate, event.comData);
 		}
 		
 		/* 객실 콤보박스 변경시 */
 		private function onRoomsCombo(event:ComboBoxRoomsEvent):void
 		{
 			var objDate:Date = view.reqView.sDay;
 			
 			var cbProxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
			cbProxy.loadRoomsCombo(objDate, event.comData, event.comData2);
 		}
 		
 		/* 용도 업무용 박스 변경시 */
 		private function onBusinessComboSearch():void
 		{
 			var objDate:Date = view.reqView.sDay;
 			
 			var cbProxy:CondoProxy = facade.retrieveProxy(CondoProxy.NAME) as CondoProxy;
			cbProxy.loadBusinessCombo(objDate);
 		}
 		
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					CondoFacade.LOAD_LIST_COMPLETE,
					CondoFacade.LOAD_LIST_FAILED,
					CondoFacade.INSERT_COMPLETE,
					CondoFacade.INSERT_FAILED,
					CondoFacade.DELETE_COMPLETE,
					CondoFacade.DELETE_FAILED,
					CondoFacade.READ_LIST_COMPLETE,
					CondoFacade.READ_LIST_FAILED,
					CondoFacade.CANCEL_REQUEST_INPUT,
					CondoProxy.LOAD_CONDOCB_COMPLTE,
					CondoProxy.LOAD_CONDOCB_FAILED,
					CondoProxy.LOAD_AREACB_COMPLTE,
					CondoProxy.LOAD_AREACB_FAILED,
					CondoProxy.LOAD_ROOMSCB_COMPLTE,
					CondoProxy.LOAD_ROOMSCB_FAILED,
					CondoProxy.LOAD_BUSINESSCB_COMPLTE,
					CondoProxy.LOAD_BUSINESSCB_FAILED,
					CondoFacade.LOAD_PROFILE_COMPLETE,
					CondoFacade.LOAD_PROFILE_FAILED
					
					]);
		}
		
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case CondoFacade.LOAD_LIST_COMPLETE:
					onLoadList(note.getBody() as CondoProxy);
					break;
				case CondoFacade.LOAD_LIST_FAILED:
					break;
				case CondoFacade.READ_LIST_COMPLETE:
					onReadList(note.getBody() as CondoProxy);
					
					break;
				case CondoFacade.READ_LIST_FAILED:
					break;	
				case CondoFacade.OK_REQUEST_INPUT:
					view.vs.selectedIndex = 0;
					loadList();
					break;
				case CondoFacade.CANCEL_REQUEST_INPUT:
					view.reqView.fileUploadChk = false;
					Alert.show((CondoProxy(note.getBody()).failedAlert));
					break;
				case CondoFacade.INSERT_COMPLETE:
					Alert.show("저장되었습니다.");
					view.reqView.clickOutputBtn();
					view.vs.selectedIndex = 0;
					loadList();
					break;
				case CondoFacade.DELETE_COMPLETE:
					loadList();
					break;
				case CondoProxy.LOAD_CONDOCB_COMPLTE:
					onCondoCb(note.getBody() as CondoProxy);
					break;
				case CondoProxy.LOAD_CONDOCB_FAILED:
					break;
				case CondoProxy.LOAD_AREACB_COMPLTE:
					onAreaCb(note.getBody() as CondoProxy);
					break;
				case CondoProxy.LOAD_AREACB_FAILED:
					break;	
				case CondoProxy.LOAD_ROOMSCB_COMPLTE:
					onRoomsCb(note.getBody() as CondoProxy);
					break;
				case CondoProxy.LOAD_ROOMSCB_FAILED:
					break;	
				case CondoProxy.LOAD_BUSINESSCB_COMPLTE:
					onBusinessCb(note.getBody() as CondoProxy);
					break;
				case CondoProxy.LOAD_BUSINESSCB_FAILED:
					break;	
				case CondoFacade.LOAD_PROFILE_COMPLETE:
					onSucceedDataLoad(note.getBody() as CondoProxy);
					break;
				case CondoFacade.LOAD_PROFILE_FAILED:
					break;					
				
					
				case CondoProxy.LOAD_CONDOCB_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "("+(note.getBody() as CondoProxy).errCode+")"+ (note.getBody() as CondoProxy).errText);
					break;
				default:
					break;
			}
		}
		
		private function onLoadYearList(proxy:CondoProxy):void
 		{
 			view.acMedYear = proxy.acMedYear;
 			loadList();
 		}
		
 		private function onLoadList(proxy:CondoProxy):void
 		{
 			view.inrecColl = proxy.condoList;
 		}
 		
 		private function onReadList(proxy:CondoProxy):void
 		{
 			view.inrecColl = proxy.condoList;
 			if(proxy.condoListArr[0].STATS == "A"){
 				view.reqView.setLoad(proxy.condoListArr[0], "EDIT");	
 			}else{
 				view.reqView.setLoad(proxy.condoListArr[0], "SEARCH");
 			}
			view.reqView.fileListArr = new ArrayCollection();
			view.reqView.fileListArr = proxy.condoFileArr;
			if(view.reqView.fileListArr.length != 0){
				view.reqView.fileCompletLen = view.reqView.fileListArr.length;	
			}
			view.vs.selectedIndex = 1;
 		}
 		
 		public function addReadTextInput(ac:ArrayCollection):void
 		{
				
 		}
 		 
		private function onCondoCb(proxy:CondoProxy):void
		{ 
			view.reqView.readCondoCb = proxy.readCondoCb;
			view.reqView.readCondoCb.addItemAt({TEXT:"선택하세요", CODE:""},0);
			view.reqView.condoCbList();
		}
		
		private function onAreaCb(proxy:CondoProxy):void
		{ 
			view.reqView.readAreaCb = proxy.readAreaCb;
			view.reqView.readAreaCb.addItemAt({TEXT:"선택하세요", CODE:""},0);
			view.reqView.areaCbList();
		}
		
		private function onRoomsCb(proxy:CondoProxy):void
		{ 
			view.reqView.readRoomsCb = proxy.readRoomsCb;
			view.reqView.readRoomsCb.addItemAt({TEXT:"선택하세요", CODE:""},0);
		}
		
		private function onBusinessCb(proxy:CondoProxy):void
		{ 
			view.reqView.readBusinessCb = proxy.readBusinessCb;
			view.reqView.readBusinessCb.addItemAt({TEXT:"선택하세요", CODE:""},0);
			view.reqView.businessCbList();
			view.reqView.comboBoxCall();
			
		}
		
		private function onSucceedDataLoad(proxy:CondoProxy):void
 		{
 			trace("onSucceedDataLoad");
 			view.reqView.dataProvider = proxy.profile;
 			onBusinessComboSearch();
 		}
 		
        private function get view():CondoreqView
        {
            return viewComponent as CondoreqView;
        }
		
		
	}
}